<template>
    <div class="system-user-container">
        <el-form :inline="true" ref="form" class="system-search-form" :model="searchForm">
            <el-form-item label="机构">
                <el-cascader
                    class="go-out"
                    v-model="searchForm.orgId"
                    :options="mechanismList"
                    :show-all-levels="false"
                    :props="{ checkStrictly: true, emitPath: false }"
                    filterable
                    size="small">
                </el-cascader>
            </el-form-item>
            <el-form-item label="MDC">
                <el-select v-model="searchForm.mdc" clearable filterable size="small">
                    <el-option
                        v-for="item in MDCList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="组类别">
                <el-select v-model="searchForm.groupType" clearable filterable size="small">
                    <el-option
                        v-for="item in groupList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="细分条件">
                <el-select v-model="searchForm.xftj" clearable filterable size="small">
                    <el-option
                        v-for="item in conditionList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="DRGs代码/名称">
                <el-input v-model="searchForm.drgCodeName" clearable size="small" class="name-input"></el-input>
            </el-form-item>
            <el-form-item label="权重">
                <el-input class="weight-input" clearable size="small" v-model="searchForm.weightStart"></el-input>
                -
                <el-input class="weight-input" clearable size="small" v-model="searchForm.weightEnd"></el-input>
            </el-form-item>
            <el-form-item label="风险等级">
                <el-select v-model="searchForm.riskLevel" filterable clearable size="small">
                    <el-option :key="0" label="全部" :value="0"></el-option>
                    <el-option :key="5" label="无死亡组" :value="5"></el-option>
                    <el-option :key="1" label="低风险组" :value="1"></el-option>
                    <el-option :key="2" label="中低风险组" :value="2"></el-option>
                    <el-option :key="3" label="中高风险组" :value="3"></el-option>
                    <el-option :key="4" label="高风险组" :value="4"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="启停">
                <el-select v-model="searchForm.isState" filterable clearable size="small">
                    <el-option :key="1" label="启用" :value="1"></el-option>
                    <el-option :key="0" label="停用" :value="0"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button class="sub-btn" type="primary" size="small" @click="toFirstPage()">查询</el-button>
            </el-form-item>
        </el-form>
        <div class="table-box">
            <div class="head">
                <h3></h3>
                <div class="btn-box">
                    <!--<el-button type="primary" icon="el-icon-upload2" size="small" v-auth="{code: 100003000004}"-->
                               <!--@click="addDRGs">新增-->
                    <!--</el-button>-->
                    <el-button type="primary" icon="el-icon-upload2" size="small" @click="importDRGs">批量导入DRGs组</el-button>
                </div>
            </div>
            <el-table
                ref="tableData"
                :data="tableData"
                tooltip-effect="light"
                border
                :max-height="tableHeight"
                @sort-change="sortChange"
                class="data-table">
                <el-table-column
                    label="序号"
                    align="center"
                    type="index"
                    width="50">
                </el-table-column>
                <el-table-column
                    prop="mdc"
                    align="center"
                    label="MDC"
                    sortable="custom"
                    width="80"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                    prop="groupType"
                    align="center"
                    label="组类别"
                    sortable="custom"
                    min-width="120">
                    <template slot-scope="scope">
                        <el-tooltip class="item" effect="light" placement="top">
                            <div slot="content">
                                {{scope.row.groupType | getGroupType}}
                            </div>
                            <span class="exceed-text">{{scope.row.groupType | getGroupType}}</span>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="drgCode"
                    align="center"
                    label="DRGs代码"
                    sortable="custom"
                    width="120"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                    prop="name"
                    align="center"
                    label="DRGs名称"
                    min-width="120"
                    sortable="custom"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                    prop="weightRw"
                    label="权重"
                    align="center"
                    width="80"
                    sortable="custom"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                    prop="benchmark"
                    align="center"
                    v-if="showTableCol"
                    width="100"
                    label="基准点数">
                </el-table-column>
                <el-table-column
                    prop="adjustmentFactor"
                    align="center"
                    v-if="showTableCol"
                    width="100"
                    label="差异系数">
                </el-table-column>
                <el-table-column
                    prop="ratePR"
                    :sortable="showTableRow ? 'custom' : false"
                    label="费率PR"
                    align="center"
                    width="160"
                    show-overflow-tooltip>
                    <template slot-scope="scope">
                        <div v-if="showTableRow">{{scope.row.ratePR}}</div>
                        <div v-if="!showTableRow">城镇职工：{{scope.row.cityRate}}</div>
                        <div v-if="!showTableRow">城乡居民：{{scope.row.townRate}}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="groupPaymentStandard"
                    align="center"
                    label="组付费标准"
                    width="160"
                    :sortable="showTableRow ? 'custom' : false"
                    show-overflow-tooltip>
                    <template slot-scope="scope">
                        <div v-if="showTableRow">{{scope.row.groupPaymentStandard | getMoneyNum}}</div>
                        <div v-if="!showTableRow">城镇职工：{{scope.row.cityGroupPayStandard | getMoneyNum}}</div>
                        <div v-if="!showTableRow">城乡居民：{{scope.row.tomnGroupPayStandard | getMoneyNum}}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="riskLevel"
                    label="风险等级"
                    sortable="custom"
                    width="110"
                    align="center"
                    show-overflow-tooltip>
                    <template slot-scope="scope">
                        {{scope.row.riskLevel | getRiskLevel}}
                    </template>
                </el-table-column>
                <el-table-column
                    label="区域(标杆值)"
                    align="center"
                    show-overflow-tooltip>
                    <el-table-column
                        prop="avgCost"
                        sortable="custom"
                        label="组均住院费用"
                        align="center"
                        width="130"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                        prop="avgTime"
                        align="center"
                        sortable="custom"
                        label="组均住院时间"
                        width="130"
                        show-overflow-tooltip>
                    </el-table-column>
                </el-table-column>
                <el-table-column
                    align="center"
                    label="启/停"
                    width="100"
                    show-overflow-tooltip>
                    <template slot-scope="scope">
                        <el-switch v-model="scope.row.state" size="small" :disabled="!showBtnAuth(300005002001)" @change="toChangeState(scope.$index, scope.row)"></el-switch>
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    align="center"
                    width="120"
                    show-overflow-tooltip>
                    <template slot-scope="scope">
                        <el-button type="text" size="small" v-auth="{code: 300005002001}" @click="tableEdit(scope.row)">
                            编辑
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="tableData.length > 0" :total="total" :page.sync="currentPage" :limit.sync="pageSize"
                        @pagination="getData"/>
        </div>
        <el-dialog
            title="编辑DRGs组"
            :visible.sync="dialogVisible"
            :close-on-click-modal="false"
            @close="closeDialog"
            center
            width="40%">
            <el-form v-if="dialogVisible" ref="dialogForm" class="dialog-form" :rules="editFormRules" :model="editForm" label-width="140px">
                <el-form-item label="MDC" prop="mdc">
                    <el-select v-model="editForm.mdc" placeholder="请选择" disabled size="small">
                        <el-option
                            v-for="item in MDCList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="组类别" prop="groupType">
                    <el-select v-model="editForm.groupType" placeholder="请选择" disabled size="small">
                        <el-option
                            v-for="item in groupList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="DRGs代码" prop="drgCode">
                    <el-input type="text" v-model="editForm.drgCode" disabled size="small"></el-input>
                </el-form-item>
                <el-form-item label="DRGs名称" prop="name">
                    <el-input type="text" v-model="editForm.name" disabled size="small"></el-input>
                </el-form-item>
                <el-form-item label="权重" prop="weightRw">
                    <el-input type="text" v-model="editForm.weightRw" size="small"></el-input>
                </el-form-item>
                <el-form-item label="差异系数" prop="adjustmentFactor" v-if="showTableCol">
                    <el-input type="text" v-model="editForm.adjustmentFactor" size="small"></el-input>
                </el-form-item>
                <el-form-item label="风险等级" prop="riskLevel">
                    <el-select v-model="editForm.riskLevel" size="small">
                        <el-option :key="1" label="低风险组" :value="1"></el-option>
                        <el-option :key="2" label="中低风险组" :value="2"></el-option>
                        <el-option :key="3" label="中高风险组" :value="3"></el-option>
                        <el-option :key="4" label="高风险组" :value="4"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="区域组均住院费用" prop="avgCost">
                    <el-input type="text" v-model="editForm.avgCost" size="small"></el-input>
                </el-form-item>
                <el-form-item label="区域组均住院时间" prop="avgTime">
                    <el-input type="text" v-model="editForm.avgTime" size="small"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="closeDialog" size="small">取 消</el-button>
                <el-button type="primary" @click="submitEditForm('dialogForm')" size="small">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog
                title="批量导入DRGs组"
                :visible.sync="dialogVisibleUpload"
                :close-on-click-modal="false"
                width="30%"
                center
                :before-close="handleCloseImport">
            <div class="content">
                <div class="template-box">
                    <el-form :inline="true" ref="form" class="drgs-import" :model="searchFormAdd">
                        <el-form-item label="更新范围：" v-show="isSystemUser">
                            <el-select v-model="searchFormAdd.province" filterable clearable placeholder="请选择省" class="city-select" @change="getCity" size="small">
                                <el-option
                                        v-for="item in provinceList"
                                        :key="item.i"
                                        :label="item.n"
                                        :value="item.i">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item v-show="isSystemUser">
                            <el-select v-model="searchFormAdd.city" filterable clearable placeholder="请选择市" class="city-select" @change="getArea" size="small">
                                <el-option
                                        v-for="item in cityList"
                                        :key="item.i"
                                        :label="item.n"
                                        :value="item.i">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="机构：">
                            <el-select v-model="searchFormAdd.orgId" filterable clearable multiple class="org-select multiselectEdit" size="small">
                                <el-option
                                        v-for="item in orgList"
                                        :key="item.id"
                                        :label="item.name"
                                        :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-form>
                    <el-button type="text" size="small" @click="downloadTemplate" icon="el-icon-download">下载模板
                    </el-button>
                </div>
                <upload-file
                        :drag="true"
                        :limit="1"
                        :isdbf="true"
                        :iscsv="true"
                        accept=".xls,.xlsx"
                        :noPrompt="true"
                        :fileList="fileList"
                        @removeFile="removeFile">
                </upload-file>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button size="small" @click="handleCloseImport">取 消</el-button>
                <el-button size="small" @click="submitFileList" type="primary">确 定</el-button>
                <div class="tips">
                    <p><i class="el-icon-info"></i>提示</p>
                    <p><span></span>批量导入权重值后，系统自动计算生成组付费标准值。</p>
                    <p><span></span>DRGs代码、权重字段为必填字段，不允许上传空值。其余字段为非必填字段，允许上传空值，空值时不更新系统内已保存值。</p>
                    <p><span></span>文件格式仅支持：.xls、.xlsx，单文件大小不超过100MB，文件数量不超过1个</p>
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {getDRGGroupList, updateGroup, getMdc, changeState, getParamsDrgData, uploadDRGsFile} from '@/api/system/drg-group'
    import {getSortString, showBtnAuth, getMoneyNum} from '@/utils/filters'
    import Pagination from '@/components/Pagination'
    import regions from '@/assets/json/regions.json'
    import uploadFile from '@/components/Upload'

    export default {
        name: 'index',
        data () {
            const validateEditForm = (rule, value, callback) => {
                if (isNaN(Number(value)) || Number(value) <= 0) {
                    callback(new Error(rule.message))
                } else {
                    callback()
                }
            }
            return {
                // 搜索内容
                searchForm: {
                    orgId: '',
                    mdc: '',
                    groupType: '',
                    xftj: '',
                    drgCodeName: '',
                    weightStart: '',
                    weightEnd: '',
                    riskLevel: '',
                    isState: 1
                },
                // 机构列表
                mechanismList: [],
                // MDC列表
                MDCList: [],
                // 组类别列表
                groupList: [{
                    value: 0,
                    label: '外科组(A,B,C,D,E,F,G,H,J)'
                }, {
                    value: 1,
                    label: '操作组(K,L,M,N,P,Q)'
                }, {
                    value: 2,
                    label: '内科组(R,S,T,U,V,W,X,Y,Z)'
                }],
                // 细分条件列表
                conditionList: [{
                    value: '0',
                    label: '小于 17 岁组'
                }, {
                    value: '1',
                    label: '伴有严重并发症与合并症'
                }, {
                    value: '3',
                    label: '伴有一般并发症与合并症'
                }, {
                    value: '5',
                    label: '不伴有并发症与合并症'
                }, {
                    value: '7',
                    label: '死亡或转院'
                }, {
                    value: '9',
                    label: '未作区分的情况'
                }],
                // 表格数据
                tableData: [],
                // 数据总数
                total: 0,
                // 当前页
                currentPage: 1,
                // 每页数量
                pageSize: 10,
                // 表格高度
                tableHeight: 100,
                // 显示弹框
                dialogVisible: false,
                // 弹框表单
                editForm: {
                    orgId: '',
                    mdc: '',
                    groupType: '',
                    drgCode: '',
                    name: '',
                    weightRw: '',
                    adjustmentFactor: '',
                    riskLevel: '',
                    avgCost: '',
                    avgTime: ''
                },
                // 排序方式
                orderBy: null,
                // 排序字段
                sortMap: {},
                // 显示列表参数
                showTableCol: false,
                // 显示费率方式
                showTableRow: true,
                editFormRules: {
                    mdc: [
                        { required: true, message: '请选择MDC', trigger: 'change' }
                    ],
                    groupType: [
                        { required: true, message: '请选择组类别', trigger: 'change' }
                    ],
                    drgCode: [
                        { required: true, message: '请填写DRGs代码', trigger: ['blur', 'change'] }
                    ],
                    name: [
                        { required: true, message: '请填写DRGs名称', trigger: ['blur', 'change'] }
                    ],
                    weightRw: [
                        { required: true, validator: validateEditForm, message: '请填写权重，为大于0的数字', trigger: ['blur', 'change'] }
                    ],
                    adjustmentFactor: [
                        { required: true, validator: validateEditForm, message: '请填写差异系数，为大于0的数字', trigger: ['blur', 'change'] }
                    ],
                    riskLevel: [
                        { required: true, message: '请选择风险等级', trigger: 'change' }
                    ],
                    avgCost: [
                        { required: true, validator: validateEditForm, message: '请填写区域组均住院费用，为大于0的数字', trigger: ['blur', 'change'] }
                    ],
                    avgTime: [
                        { required: true, validator: validateEditForm, message: '请填写区域组均住院时间，为大于0的数字', trigger: ['blur', 'change'] }
                    ]
                },
                // 使用储存的数据，不去请求
                useStoreData: false,
                useStoreMDC: false,
                useStoreParams: false,
                // 批量上传弹框显示
                dialogVisibleUpload: false,
                // 上传列表
                fileList: [],
                // 省列表
                provinceList: [],
                // 市列表
                cityList: [],
                // 导入选择表单
                searchFormAdd: {
                    province: '',
                    city: '',
                    orgId: []
                },
                // 导入选择机构列表
                orgList: [],
                // 是否是系统用户
                isSystemUser: false
            }
        },
        created () {
            // tags判断入口是否从选项卡进入
            this.useStoreData = this.$route.query.tags === 'tags'
            this.useStoreMDC = this.$route.query.tags === 'tags'
            this.useStoreParams = this.$route.query.tags === 'tags'
            this.getMdcList()
            let height = document.documentElement.clientHeight - 213 - 10 - 78 - 5 - 33
            this.tableHeight = height > 107 ? height : 107
            this.getAddressList()
            this.getMechanismList()
            this.initData()
            this.getData()
            this.isSystemUser = this.getLocalStorage('userMechanism', 'name') === '系统机构'
        },
        mounted () {
            window.onresize = () => {
                // 213=其他元素高度60+50+48+55 10=距离底部边距 78=分页组件高度 5=适应性调整
                let height = document.documentElement.clientHeight - 213 - 10 - 78 - 5 - 33
                this.tableHeight = height > 107 ? height : 107
            }
        },
        methods: {
            showBtnAuth,
            /**
             * 获取省列表
             */
            getAddressList () {
                this.provinceList = regions.a
                this.orgList = this.getOrgList()
            },
            /**
             * 获取市列表
             * @param val 选中的id
             */
            getCity (val) {
                this.searchFormAdd.city = ''
                this.searchFormAdd.area = ''
                this.cityList = this.getChildren(this.provinceList, val)
                this.orgList = this.getOrgList(val)
            },
            /**
             * 获取区列表
             * @param val 选中的id
             */
            getArea (val) {
                this.searchFormAdd.area = ''
                this.orgList = this.getOrgList(this.searchFormAdd.province, val)
            },
            /**
             * 遍历获取地区子级
             * @param arr 当前地区列表
             * @param id 指定id
             */
            getChildren (arr, id) {
                let length = arr.length
                let list = []
                for (let i = 0; i < length; i++) {
                    if (arr[i].i === id) {
                        list = arr[i].c
                        break
                    }
                }
                return list
            },
            /**
             * 获取当前地区机构
             * @param province 省
             * @param city 市
             */
            getOrgList (province, city) {
                let mechanismList = this.getLocalStorage('userInfo', 'mechanism')
                return mechanismList.filter(item => {
                    if (province && !city) {
                        return item.province === province
                    } else if (province && city) {
                        return item.province === province && item.city === city
                    } else {
                        return item
                    }
                })
            },
            /**
             * 初始化页面参数
             */
            initData () {
                let query = JSON.parse(JSON.stringify(this.$store.state.search.searchForm[this.$route.name]))
                if (JSON.stringify(query) !== '{}') {
                    this.searchForm = {
                        orgId: query.orgId,
                        mdc: query.mdc || '',
                        groupType: query.groupType || '',
                        xftj: query.xftj || '',
                        drgCodeName: query.drgCodeName || '',
                        weightStart: query.weightStart || '',
                        weightEnd: query.weightEnd || '',
                        riskLevel: query.riskLevel || '',
                        isState: query.isState
                    }
                    this.currentPage = query.index || 1
                    this.pageSize = query.pageSize || 10
                    this.orderBy = query.orderBy || null
                }
            },
            /**
             * 查询参数配置
             * @param form 筛选条件
             */
            getParams (form) {
                if (this.getSession(this.$route.name, 'getParamsDrgData') && this.useStoreParams) {
                    this.useStoreParams = false
                    let res = this.getSession(this.$route.name, 'getParamsDrgData')
                    if (res.code.toString() === '200') {
                        let arr = res.data.filter(item => item.name === 'DRG测算模式与费率')
                        if (arr.length) {
                            this.showTableRow = JSON.parse(arr[0].value).费率方式 === '通用费率'
                            this.showTableCol = JSON.parse(arr[0].value).测算模式 === 'DRGs病组点数法'
                        } else {
                            this.showTableRow = false
                            this.showTableCol = false
                        }
                    }
                } else {
                    getParamsDrgData(form.orgId).then(res => {
                        this.useStoreParams = false
                        this.setSession(this.$route.name, {getParamsDrgData: res})
                        if (res.code.toString() === '200') {
                            let arr = res.data.filter(item => item.name === 'DRG测算模式与费率')
                            if (arr.length) {
                                this.showTableRow = JSON.parse(arr[0].value).费率方式 === '通用费率'
                                this.showTableCol = JSON.parse(arr[0].value).测算模式 === 'DRGs病组点数法'
                            } else {
                                this.showTableRow = false
                                this.showTableCol = false
                            }
                        }
                    })
                }
            },
            /**
             * 获取机构列表
             */
            getMechanismList () {
                let mechanismList = this.getLocalStorage('userInfo', 'mechanism')
                let parentList = []
                mechanismList.forEach(item => {
                    // 一级机构
                    if (!item.pid) {
                        parentList.push({
                            value: item.id,
                            label: item.name,
                            children: []
                        })
                        // 有一级机构的二级机构
                        mechanismList.forEach(it => {
                            if (it.pid === item.id) {
                                parentList[parentList.length - 1].children.push({
                                    value: it.id,
                                    label: it.name,
                                    children: null
                                })
                            }
                        })
                        if (!parentList[parentList.length - 1].children.length) {
                            parentList[parentList.length - 1].children = null
                        }
                    } else {
                        // 二级机构
                        let parent = mechanismList.filter(i => item.pid === i.id)
                        if (!parent.length) {
                            parentList.push({
                                value: item.id,
                                label: item.name,
                                children: null
                            })
                        }
                    }
                })
                this.mechanismList = parentList
                this.searchForm.orgId = parentList[0].value
            },
            /**
             * 启动停止
             * @param index 索引
             * @param row 行数据
             */
            toChangeState (index, row) {
                let text = ''
                let title = ''
                if (row.state) {
                    text = `您确定启用"${row.name}"DRGs组吗?`
                    title = '启用DRGs组'
                } else {
                    text = `您确定停用"${row.name}"DRGs组吗?`
                    title = '停用DRGs组'
                }
                this.$confirm(text, title, {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                }).then(() => {
                    this.changeIdState(row, row.state)
                }).catch(() => {
                    row.state = !row.state
                })
            },
            /**
             * 改变指定状态
             * @param row 行数据
             * @param state 状态
             */
            changeIdState (row, state) {
                let params = {
                    DrgCode: row.drgCode,
                    orgId: row.orgId || this.searchForm.orgId,
                    State: Boolean(state)
                }
                changeState(params).then(res => {
                    if (res.code.toString() === '200') {
                        this.$message.success(state ? '启用成功' : '停用成功')
                    } else {
                        this.$message.error(res.message)
                    }
                    this.getData()
                })
            },
            /**
             * 获取mdc列表
             */
            getMdcList () {
                if (this.getSession(this.$route.name, 'getMdc') && this.useStoreMDC) {
                    this.useStoreMDC = false
                    let res = this.getSession(this.$route.name, 'getMdc')
                    if (res.code.toString() === '200') {
                        for (let key in res.data) {
                            this.MDCList.push({
                                label: key.toUpperCase() + ' ' + res.data[key],
                                value: key.toUpperCase()
                            })
                        }
                    }
                } else {
                    getMdc().then(res => {
                        this.useStoreMDC = false
                        this.setSession(this.$route.name, {getMdc: res})
                        if (res.code.toString() === '200') {
                            for (let key in res.data) {
                                this.MDCList.push({
                                    label: key.toUpperCase() + ' ' + res.data[key],
                                    value: key.toUpperCase()
                                })
                            }
                        }
                    })
                }
            },
            /**
             * 获取列表
             */
            getData () {
                let params = {
                    ...this.searchForm,
                    index: this.currentPage,
                    pageSize: this.pageSize,
                    orderBy: this.orderBy
                }
                this.getParams(params)
                params.groupType = params.groupType || null
                params.weightStart = Number(params.weightStart) || null
                params.weightEnd = Number(params.weightEnd) || null
                params.isState = params.isState === '' ? null : params.isState
                params.riskLevel = params.riskLevel || null
                this.setGetStore(this.$route.name, params)
                if (this.getSession(this.$route.name, 'getDRGGroupList') && this.useStoreData) {
                    this.useStoreData = false
                    let res = this.getSession(this.$route.name, 'getDRGGroupList')
                    if (res.code.toString() === '200') {
                        this.tableData = []
                        res.data.rows.forEach(item => {
                            this.tableData.push({
                                ...item,
                                state: Boolean(item.state)
                            })
                        })
                        this.sortMap = res.data.maps
                        this.total = res.data.total
                    }
                } else {
                    getDRGGroupList(params).then(res => {
                        this.useStoreData = false
                        this.setSession(this.$route.name, {getDRGGroupList: res})
                        if (res.code.toString() === '200') {
                            this.tableData = []
                            res.data.rows.forEach(item => {
                                this.tableData.push({
                                    ...item,
                                    state: Boolean(item.state)
                                })
                            })
                            this.sortMap = res.data.maps
                            this.total = res.data.total
                        }
                    })
                }
            },
            /**
             * 修改条件后从第一页开始
             */
            toFirstPage () {
                this.currentPage = 1
                this.getData()
            },
            /**
             * 排序修改
             * @param colData.prop 排序参数
             * @param colData.order 正序倒序 ascending 升序 descending 降序 null 不排序默认
             */
            sortChange (colData) {
                this.tableData = []
                this.orderBy = getSortString(colData, this.sortMap)
                this.currentPage = 1
                this.getData()
            },
            /**
             * 编辑表格内容
             * @param row 行数据
             */
            tableEdit (row) {
                this.editForm = {
                    orgId: row.orgId || this.searchForm.orgId,
                    mdc: row.mdc,
                    groupType: row.groupType,
                    drgCode: row.drgCode,
                    name: row.name,
                    weightRw: row.weightRw,
                    adjustmentFactor: row.adjustmentFactor,
                    riskLevel: row.riskLevel,
                    avgCost: row.avgCost,
                    avgTime: row.avgTime
                }
                this.dialogVisible = true
            },
            /**
             * 关闭弹框
             */
            closeDialog () {
                this.editForm = {
                    orgId: '',
                    mdc: '',
                    groupType: '',
                    drgCode: '',
                    name: '',
                    weightRw: '',
                    adjustmentFactor: '',
                    riskLevel: '',
                    avgCost: '',
                    avgTime: ''
                }
                this.$refs['dialogForm'].resetFields()
                this.dialogVisible = false
            },
            /**
             * 提交编辑内容
             * @param formName 表单名
             */
            submitEditForm (formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let params = this.editForm
                        params.weightRw = Number(params.weightRw)
                        params.adjustmentFactor = params.adjustmentFactor ? Number(params.adjustmentFactor) : params.adjustmentFactor
                        params.avgCost = Number(params.avgCost)
                        params.avgTime = Number(params.avgTime)
                        updateGroup(params).then(res => {
                            if (res.code.toString() === '200') {
                                this.$message.success('修改成功')
                                this.closeDialog()
                                this.getData()
                            } else {
                                this.$message.error(res.message)
                            }
                        })
                    } else {
                        return false
                    }
                })
            },
            /**
             * 关闭前
             */
            handleCloseImport () {
                if (this.fileList.length > 0) {
                    this.$confirm('还未上传，是否关闭？')
                        .then(() => {
                            this.closeDialogImport()
                        })
                        .catch(() => {})
                } else {
                    this.closeDialogImport()
                }
            },
            /**
             * 关闭弹窗
             */
            closeDialogImport () {
                this.fileList = []
                this.dialogVisibleUpload = false
                this.searchFormAdd = {
                    province: '',
                    city: '',
                    orgId: []
                }
                this.orgList = []
                this.cityList = []
            },
            /**
             * 显示上传弹框
             * @param index 索引
             * @param row 行数据
             */
            importDRGs (index, row) {
                this.fileDataId = row ? row.packageID : ''
                this.fileList = []
                this.dialogVisibleUpload = true
                this.getAddressList()
            },
            /**
             * 下载模板
             */
            downloadTemplate () {
                window.location.href = '/mrms/static/批量导入DRGs组模板.xlsx'
            },
            /**
             * 删除文件
             * @param uid 要删除的文件
             */
            removeFile (uid) {
                let index = this.fileList.findIndex(item => {
                    return item.uid === uid
                })
                if (index !== -1) {
                    this.fileList.splice(index, 1)
                }
            },
            /**
             * 提交上传文件
             */
            submitFileList () {
                if (this.fileList.length === 0) {
                    this.$message.warning('请先上传文件')
                    return false
                } else {
                    let form = new FormData()
                    for (let i = 0; i < this.fileList.length; i++) {
                        form.append('file', this.fileList[i].raw, this.fileList[i].raw.name)
                    }
                    // 单文件
                    if (this.searchFormAdd.orgId.length) {
                        form.append('orgId', JSON.stringify(this.searchFormAdd.orgId))
                    } else {
                        form.append('orgId', JSON.stringify(this.orgList.map(item => item.id)))
                    }
                    uploadDRGsFile(form, this.fileDataId).then(res => {
                        if (res.code.toString() === '200') {
                            this.getData()
                            this.closeDialogImport()
                            this.$message.success('导入成功')
                        } else {
                            this.$message.error(res.message)
                        }
                    })
                }
            }
        },
        components: {
            Pagination,
            uploadFile
        },
        filters: {
            getMoneyNum,
            getGroupType (val) {
                switch (val) {
                case 0:
                    return '外科组(A,B,C,D,E,F,G,H,J)'
                case 1:
                    return '操作组(K,L,M,N,P,Q)'
                case 2:
                    return '内科组(R,S,T,U,V,W,X,Y,Z)'
                }
            },
            getRiskLevel (val) {
                switch (val) {
                case 0:
                    return ''
                case 1:
                    return '低风险组'
                case 2:
                    return '中低风险组'
                case 3:
                    return '中高风险组'
                case 4:
                    return '高风险组'
                case 5:
                    return '无死亡组'
                }
            }
        },
        beforeDestroy () {
            window.onresize = null
        }
    }
</script>

<style lang="scss" scoped>
    .system-user-container {
        .system-search-form {
            .el-form-item {
                margin-bottom: 0;
                margin-right: 5px;
                /deep/.el-form-item__label {
                    padding-right: 6px;
                }
                /deep/.el-input {
                    width: 90px;
                    input {
                        padding: 0 5px;
                    }
                }
                .weight-input {
                    width: 50px;
                }
            }
        }
        .table-box {
            background: #fff;
            padding: 10px;
            box-sizing: border-box;
            height: calc(100vh - 183px);
            .head {
                display: flex;
                justify-content: space-between;
                align-items: center;
                margin-bottom: 10px;
                .btn-box {
                    display: flex;
                    justify-content: flex-end;
                    align-items: center;
                }
            }
        }
        /deep/.el-dialog {
            width: 800px!important;
            .el-dialog__body {
                padding: 10px;
                .dialog-form {
                    width: 90%;
                    margin: 0 auto;
                    .form-linked-tips {
                        width: 100%;
                        .text {
                            margin: 0;
                            color: #ff8100;
                        }
                    }
                }
                .content {
                    width: 90%;
                    margin: 0 auto;
                    .template-box {
                        display: flex;
                        justify-content: space-between;
                        align-items: center;
                        margin: 0 auto 20px;
                        .el-form-item {
                            margin-bottom: 0;
                        }
                        .drgs-import {
                            .city-select {
                                width: 120px;
                            }
                            .org-select {
                                width: 160px;
                            }
                        }
                    }
                }
            }
            .el-dialog__footer {
                text-align: right;
            }
            .dialog-footer {
                text-align: center;
                .tips {
                    margin-top: 20px;
                    text-align: left;
                    padding: 10px 20px;
                    border: 1px solid #D7D7D7;
                    background: #F2F2F2;
                    border-radius: 8px;
                    p {
                        color: #333;
                        font-size: 14px;
                        line-height: 24px;
                        position: relative;
                        padding-left: 20px;
                        margin: 0;
                        .el-icon-info {
                            color: #108EE9;
                            margin-right: 6px;
                        }
                        &:first-child {
                            padding-left: 0;
                        }
                        span {
                            position: absolute;
                            top: 9px;
                            left: 8px;
                            display: block;
                            width: 6px;
                            height: 6px;
                            border-radius: 50%;
                            background: #333;
                        }
                    }
                }
            }
        }
    }
</style>
<style lang="scss">
    .exceed-text {
        display: block;
        max-width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    .multiselectEdit {
        .el-select__tags {
            display: flex;
            justify-content: flex-start;
            align-items: center;
            flex-wrap: nowrap;
            overflow-x: hidden;
            & > span {
                display: flex;
                justify-content: flex-start;
                align-items: center;
                flex-wrap: nowrap;
                overflow-x: hidden;
            }
        }
    }
</style>
